from fastapi import APIRouter, Depends
from fastapi.security import OAuth2PasswordRequestForm
from fastapi import Request

from conf.config import BusinessType
from core.database import DBSession
from core.log_decorator import sys_log
from core.response import create_response
from core.security import get_password_hash
from schemas.admin_schemas.admin_schema import Token, LoginIn
from schemas.common_schema import ResponseBase
from services.admin_services.admin_service import AdminService

admin_login_router = APIRouter()


@admin_login_router.post("/login", summary="获取授权token", response_model=ResponseBase[Token])
@sys_log(title='登录', business_type=BusinessType.OTHER, log_type="login")
async def login(request: Request, db: DBSession,  login_in: LoginIn):
    """
    用户登录
    :param request
    :param db: db_session
    :param login_in: 登录参数
    :return:
    """
    admin_service = AdminService(db)
    token = await admin_service.do_login(request, login_in.username, login_in.password)
    return create_response(data=token)


@admin_login_router.post("/access-token", tags=["登录相关"], summary="swagger获取token")
@sys_log(title='登录', business_type=BusinessType.OTHER, log_type='login')
async def get_access_token(request: Request, db: DBSession, form_data: OAuth2PasswordRequestForm = Depends()):
    """
    文档测试请求token方法
    :param request:
    :param db: db_session
    :param form_data:
    :return:
    """
    a = get_password_hash("abc123456")
    print(a)
    admin_service = AdminService(db)
    token = await admin_service.do_login(request, form_data.username, form_data.password)
    return {"access_token": token.token, "token_type": "bearer"}
